%this function reduces the first and second dimensions of a 3D matrix
%according to a window provided by the user
%windowProcess : [left_index right_index top_index bottom_index]
%J , I: first and second dimensions in the original matrix
%U: 3D matrix to process
%Ured: reduced matrix

%created from WindowFile.m by Ricardo Mejia-Alvarez.
%University of Illinois at Urbana-Champaign.
%Urbana, IL. 09/20/2010

function [Ired,Jred,Ured] = WindowFileMat(windowProcess,I,J,U)

L = size( U );
if length( L ) == 2
    L = 1;
    U( : , : , 1) = U;
elseif length( L ) == 3
    L = L(3);
end

Ired = I - windowProcess(1) - windowProcess(2) + 2;
Jred = J - windowProcess(3) - windowProcess(4) + 2;
Ured = zeros( Jred , Ired , L ); 

for k = 1 : L
    
    Ured(: , : , k) = U(windowProcess(3) : J - windowProcess(4) + 1 , windowProcess(1) : I - windowProcess(2) + 1 , k); 
    
end

end